package com.joplus.housekeeper.adapter;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;

import com.baiiu.filter.adapter.MenuAdapter;
import com.baiiu.filter.adapter.SimpleTextAdapter;
import com.baiiu.filter.interfaces.OnFilterDoneListener;
import com.baiiu.filter.interfaces.OnFilterItemClickListener;
import com.baiiu.filter.typeview.SingleListView;
import com.baiiu.filter.util.UIUtil;
import com.baiiu.filter.view.FilterCheckedTextView;
import com.joplus.housekeeper.utils.FilterUrl;

import java.util.List;

/**
 * author： ccm
 * time： 2017/5/2.
 * description：
 */

public class DropSpecialMenuAdapter implements MenuAdapter {
    private Context mContext;
    private String[] titleList;
    private OnFilterDoneListener onFilterDoneListener;
    private SingleListView<String> areaView;
    private SingleListView<String> degree;
    private List<String> areaList;

    public DropSpecialMenuAdapter(Context context, String[] titleList, OnFilterDoneListener onFilterDoneListener) {
        this.mContext = context;
        this.titleList = titleList;
        this.onFilterDoneListener = onFilterDoneListener;
    }

    @Override
    public int getMenuCount() {
        return titleList.length;
    }

    @Override
    public String getMenuTitle(int position) {
        return titleList[position];
    }

    @Override
    public int getBottomMargin(int position) {
        if (position == 2) {
            return 0;
        }

        return UIUtil.dp(mContext, 140);
    }

    @Override
    public View getView(int position, FrameLayout parentContainer) {
        View view = parentContainer.getChildAt(position);
        switch (position) {
            case 0:
                view = createArea();
                break;

            case 1:
                view = createListDegree();
                break;

        }

        return view;
    }


    private View createArea() {


        areaView = new SingleListView<String>(mContext)
                .adapter(new SimpleTextAdapter<String>(null, mContext) {
                    @Override
                    public String provideText(String string) {
                        return string;
                    }

                    @Override
                    protected void initCheckedTextView(FilterCheckedTextView checkedTextView) {
                        int dp = UIUtil.dp(mContext, 15);
                        checkedTextView.setPadding(dp, dp, 0, dp);
                    }
                })
                .onItemClick(new OnFilterItemClickListener<String>() {
                    @Override
                    public void onItemClick(int item) {
                        FilterUrl.instance().singleListPosition = item + "";

                        FilterUrl.instance().position = 0;
                        FilterUrl.instance().positionTitle = areaList.get(item);

                        onFilterDone();
                    }
                });
        return areaView;
    }



    private View createListDegree() {
        degree = new SingleListView<String>(mContext)
                .adapter(new SimpleTextAdapter<String>(null, mContext) {
                    @Override
                    public String provideText(String string) {
                        return string;
                    }

                    @Override
                    protected void initCheckedTextView(FilterCheckedTextView checkedTextView) {
                        int dp = UIUtil.dp(mContext, 15);
                        checkedTextView.setPadding(dp, dp, 0, dp);
                    }
                })
                .onItemClick(new OnFilterItemClickListener<String>() {
                    @Override
                    public void onItemClick(int item) {
                        FilterUrl.instance().singleListPosition = item + "";

                        FilterUrl.instance().position = 1;
                        FilterUrl.instance().positionTitle = degreelist.get(item);

                        onFilterDone();
                    }
                });


        return degree;
    }

    List<String> degreelist;

    public void setDegree(List<String> list) {
        degreelist = list;
        degree.setList(list, -1);
    }

    private void onFilterDone() {
        if (onFilterDoneListener != null) {
            onFilterDoneListener.onFilterDone(0, "", "");
        }
    }


    public void setArea(List<String> list) {
        areaList = list;
        areaView.setList(list, -1);

    }
}
